package com.yonyougov.devops.cruxcisupport.k8s.rest.service.impl;

import com.yonyougov.devops.cruxcisupport.k8s.K8sRepository;
import com.yonyougov.devops.cruxcisupport.k8s.error.K8sConfIgNotFindException;
import com.yonyougov.devops.cruxcisupport.k8s.pod.PodRepository;
import com.yonyougov.devops.cruxcisupport.k8s.rest.service.K8sPodService;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class K8sPodServiceImpl implements K8sPodService {
    @Autowired
    private K8sRepository k8sRepository;
    @Autowired
    private PodRepository podRepository;

    @Override
    public boolean deletePod(String envName, String appName) throws K8sConfIgNotFindException {
        KubernetesClient defaultClient = k8sRepository.createDefaultClient(envName);
        List<String> podNames = podRepository.getPodNames(defaultClient,envName, appName);
        for (String podName : podNames) {
            podRepository.delete(defaultClient,envName,podName);
        }
        return true;
    }
}
